package com.dengpp.pdform.base;

import android.content.Context;
import android.view.View;

import com.dengpp.pdform.api.CustomFormView;
import com.dengpp.pdform.config.AttributeConfig;

/**
 * 创建自定义视图基类
 */
public abstract class BaseCustomFormView implements CustomFormView {
    protected Context context;
    protected AttributeConfig attribute;
    protected OnValueChangeListener listener;

    @Override
    public View createView(Context context) {
        this.context = context;
        return onCreateView(context);
    }

    @Override
    public void bindData(AttributeConfig attribute, Object value) {
        this.attribute = attribute;
        onBindData(value);
    }

    @Override
    public void setOnValueChangeListener(OnValueChangeListener listener) {
        this.listener = listener;
    }

    protected abstract View onCreateView(Context context);

    protected abstract void onBindData(Object value);

    protected void notifyValueChanged(Object value) {
        if (listener != null && attribute != null) {
            listener.onValueChanged(value);
        }
    }
}
